package cn.gok.server.service.impl;

import cn.gok.common.dao.goods.GoodsMapper;
import cn.gok.common.entity.goods.Goods;
import cn.gok.server.service.GoodsService;
import cn.gok.utils.DateUtil;
import cn.gok.utils.UserContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author: DxlinY
 * @apiNote:
 * @date: 2020/11/4
 * @time: 17:44
 */
@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {


    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public PageInfo pageInfo(Object target) {
        QueryWrapper <Goods> values = new QueryWrapper <>();
        values.setEntity((Goods) target);
        return new PageInfo(goodsMapper.selectList(values));
    }

    @Override
    public int delById(List <String> ids) {
        return goodsMapper.deleteBatchIds(ids);
    }

    @Override
    public int add(Goods goods) {
        goods.setGoodsId(DateUtil.byPatternNowToStr("yyMMddhhmmss"));
        goods.setCreateBy(UserContext.user().getUserName());
        goods.setUpdateBy(UserContext.user().getUserName());
        goods.setUpdateTime(DateUtil.nowDateTime());
        goods.setCreateTime(DateUtil.nowDateTime());
        goods.setGoodsSn(UUID.randomUUID().toString());
        return goodsMapper.insert(goods);
    }

    @Override
    public Goods getById(String goodsId) {
        return goodsMapper.selectById(goodsId);
    }

    @Override
    public int updateById(Goods goods) {
        return goodsMapper.updateById(goods);
    }
}
